#include "dataStructuresHeader.h"
#include "hashTable.h"
int hashTableMethod(descriptors *desc,int iterDescriptors) 
{
	int i,ret=-1;
	hashtable=NULL;
	node *insertVal;
	char *fileName; // holds the file name 
	long blkNo; // holds the block number 
	char *filePath; // holds the path of the 
	char type;//type of FileDescriptor
	hashtable = create_ht(HASH_TABLE_SIZE); //creates a Hash table
	
	if(iterDescriptors > 0) 
	{
		for(i = 0;i < iterDescriptors;i++){
			fileName = desc[i].fileName;
			blkNo = desc[i].blockNumber;
			filePath = desc[i].location;
			type=desc[i].type;
			if(type=='f' || type=='d')
				insertVal = insert_ht(hashtable,fileName,blkNo,filePath);
		}
	}
	ret=0;
	return ret;
}

node *insert_ht(ht *hashtable,char *str,int blockNo,char *filePath)
{
	node *temp;
	int hashval=0;
	if(str!=NULL)
		hashval = hashfunc(hashtable,str[0]);
	
	if((temp = (node*)malloc(sizeof(node))) == NULL)
		return NULL;
	//printf("Hashval %d\n",hashval);
	temp->id = hashval;
	temp->filename = strdup(str);
	temp->blockNo = blockNo;
	temp->filePath = filePath;
	temp->next = hashtable->table[hashval];
	hashtable->table[hashval] = temp;
	return temp;
}

int hashfunc(ht *hashtable, char c)
{
	int i=0;
	if(c>=97 && c<=122)
	{
		i=c;
		i=i-71;
	}
	else if(c>=65 && c<=90)
	{
		i=c;
		i=i-65;
	}
	else
	{
		i=52;
	}
	return i;
}	
ht *create_ht(int size)
{	
	ht *newhashtable;
	int i;
	
	if(size < 1) 
		return NULL;
	//allocate the table
	if((newhashtable = malloc (sizeof(ht))) == NULL)
		return NULL;
	//allocate pointer to head node
	if((newhashtable->table = malloc(sizeof(node *)*size)) == NULL)
		return NULL;
	for(i=0;i<size;i++){
		newhashtable->table[i] = NULL;
	}
	
	newhashtable->size = size;
	
	return newhashtable;
}
